let JSONResponse = require("../commons/JSONResponse")
let settings = require("../commons/settings")
const https = require('https');
const { PassThrough } = require('stream');
const imageUrlList = [
    "https://gitee.com/wx_971d548fca/images/raw/master/p12d01.png",
    "https://gitee.com/wx_971d548fca/images/raw/master/p1d01.png",
    "https://gitee.com/wx_971d548fca/images/raw/master/p7d01.jpg"
]
function convertImageToBase64(url) {
    return new Promise((resolve, reject) => {
        const req = https.get(url, (res) => {
            if (res.statusCode !== 200) {
                reject(new Error(`请求失败，状态码: ${res.statusCode}`));
                return;
            }

            const dataStream = new PassThrough();
            res.pipe(dataStream);

            const chunks = [];
            dataStream.on('data', (chunk) => {
                chunks.push(chunk);
            });

            dataStream.on('end', () => {
                const buffer = Buffer.concat(chunks);
                const base64 = buffer.toString('base64');
                resolve(base64);
            });

            dataStream.on('error', (err) => {
                reject(err);
            });
        });

        req.on('error', (err) => {
            reject(err);
        });
    });
}
/**
 * 获取录播图对象数组
 * @param req 
 * @param resp 
 */

exports.list = async (req, resp) => {
    try {
        const promises = imageUrlList.map(imageUrl => convertImageToBase64(imageUrl));
        const imageList = await Promise.all(promises);
        resp.json(JSONResponse.success(imageList, 200, "获取轮播图列表成功"));
    } catch (err) {
        console.error('转换过程中出现错误:', err);
        resp.json(JSONResponse.fail("获取列表失败", 500));
    }
};